package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	response "hr-system/Response"
	"time"
)

type IResignEmployeeController interface {
	RestController
	Update(c *gin.Context)
	Delete(c *gin.Context)
}

type ResignEmployeeController struct {
	DB *gorm.DB
}



func NewResignEmployeeController() IResignEmployeeController{
	db := Global.GetDB()
	return ResignEmployeeController{DB: db}
}

func (r ResignEmployeeController) PageList(c *gin.Context) {
	var requestEmployee Model.Employee
	_ = c.ShouldBind(&requestEmployee)
	name := requestEmployee.Name
	var records []Model.ResignedEmployee
	r.DB.Order("id").Find(&records)
	length := len(records)
	returnEmployee := make([]Model.ReturnResign, 0)
	for i:=0; i < length; i++ {
		re := records[i]
		var temp Model.Employee
		r.DB.Model(&Model.Employee{}).Where("id=?",re.Id).First(&temp)
		if name != "" {
			if temp.Name != name {
				continue
			}
		}
		returnEmployee = append(returnEmployee, Model.ReturnResign{
			Id: temp.Id,
			Name: temp.Name,
			Sex: temp.Sex,
			ResignationDate: re.ResignationDate.Format("2006-01-02"),
			ReasonsForResignation: re.ReasonsForResignation,
		})
	}
	response.Success(c, gin.H{"data": returnEmployee}, "查询成功")
}

func (r ResignEmployeeController) Create(c *gin.Context) {
		var requestEmployee Model.ReturnResign
		_ = c.ShouldBind(&requestEmployee)
		updateEmployee := Model.Employee{}
		result := r.DB.Where("id=?",requestEmployee.Id).First(&updateEmployee).RowsAffected
		if result == 0 {
			response.Fail(c, "该员工不存在", nil)
			return
		}
		if updateEmployee.IsQuit == "是" {
			response.Fail(c, "该员工已离职", nil)
			return
		}
		t, _ := time.Parse("2006-01-02", requestEmployee.ResignationDate)
		createResign := Model.ResignedEmployee{
			Id: requestEmployee.Id,
			ResignationDate: t,
			ReasonsForResignation: requestEmployee.ReasonsForResignation,
		}
		err := r.DB.Create(&createResign).Error
		if err != nil {
			response.Fail(c, err.Error(), nil)
			return
		}
		r.DB.Model(&updateEmployee).Update("is_quit","是")
		if err != nil {
			response.Fail(c, err.Error(), nil)
			return
		}
		response.Success(c, gin.H{"data": requestEmployee}, "操作成功")
}

func (r ResignEmployeeController) Update(c *gin.Context) {
	var requestEmployee Model.ReturnResign
	_ = c.ShouldBind(&requestEmployee)
	t, _ := time.Parse("2006-01-02", requestEmployee.ResignationDate)
	updateEmployee := Model.ResignedEmployee{
		Id: requestEmployee.Id,
		ResignationDate: t,
		ReasonsForResignation: requestEmployee.ReasonsForResignation,
	}
	err := r.DB.Model(&Model.ResignedEmployee{}).Where("id=?",requestEmployee.Id).
		Updates(&updateEmployee).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestEmployee}, "修改成功")
}

func (r ResignEmployeeController) Delete(c *gin.Context) {
	var requestEmployee Model.ReturnResign
	_ = c.ShouldBind(&requestEmployee)
	err := r.DB.Model(&Model.Employee{}).Where("id=?",requestEmployee.Id).
		Update("is_quit", "否").Error
	if err != nil{
		response.Fail(c, err.Error(), nil)
		return
	}
	err = r.DB.Delete(&Model.ResignedEmployee{}, requestEmployee.Id).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestEmployee}, "删除成功")
}
